/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "eventfilterundoredo.h"

#include <QEvent>
#include <QKeyEvent>

#include <QtDebug>

EventFilterUndoRedo::EventFilterUndoRedo()
{
}

bool EventFilterUndoRedo::eventFilter(QObject *obj, QEvent *event)
{
	if (event && event->type() == QEvent::KeyPress)
	{
		QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

		if (keyEvent && keyEvent->matches(QKeySequence::Undo))
		{
			qDebug() << "undo pressed";
			emit undo();
			return true;
		}
		else if (keyEvent && keyEvent->matches(QKeySequence::Redo))
		{
			qDebug() << "Redo pressed";
			emit redo();
			return true;
		}
	}

	return QObject::eventFilter(obj, event);

}

void EventFilterUndoRedo::installIn(QObject* target,  QObject* listener)
{
	connect(this, SIGNAL(undo()), listener, SLOT(undo()));
	connect(this, SIGNAL(redo()), listener, SLOT(redo()));
	
	target->installEventFilter(this);
}
